package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.pojo.Forum;
import com.hcrazy.manchester.app.pojo.Group;
import com.hcrazy.manchester.app.pojo.PageModel;

public class ForumDAO extends BaseDAO<Forum>{

	public void addForum(Forum forum) throws Exception{
		StringBuffer sb = new StringBuffer();
		sb.append("insert into him_forum_forum(");
		sb.append("type,name,status,displayOrder,parentId,description_mc,forumImg,memo,sortId");
		sb.append(") values (");
		sb.append(":type,:name,:status,:displayOrder,:parentId,:description_mc,:forumImg,:memo,:sortId");
		sb.append(")");
		
		update(sb.toString(), forum);
	}
	
	public void updateForum(Forum forum) throws Exception{
		StringBuffer sb = new StringBuffer();
		sb.append("update him_forum_forum set ");
		sb.append(" type=:type,name=:name,displayOrder=:displayOrder,status=:status,");
		sb.append(" description_mc=:description_mc,forumImg=:forumImg,memo=:memo,sortId=:sortId");
		sb.append(" where forumId = :forumId");
		
		update(sb.toString(), forum);
	}
	
	/**
	 * 根据status获取论坛、群组
	 * @param status status=1 论坛；status=2 群组
	 * @return
	 * @throws Exception
	 */
	public List<Forum> listAllForums(int status) throws Exception{
		String sql = "select * from him_forum_forum where status = ?";
		return getList(sql, Forum.class,status);
	}
	
	public List<Forum> listAllForums() throws Exception{
		String sql = "select * from him_forum_forum where status <> 2";
		return getList(sql,Forum.class);
	}
	
	public List<Forum> getByParentId(int parentId) throws Exception{
		String sql = "select * from him_forum_forum where parentId = ?";
		return getList(sql, Forum.class,parentId);
	}
	
	public void delForum(int forumId) throws Exception{
		String sql = "delete from him_forum_forum where forumId = ?";
		this.commonUpdate(sql, forumId);
	}
	
	public void delForumByParentId(int parentId) throws Exception{
		String sql = "delete from him_forum_forum where parentId = ?";
		this.commonUpdate(sql, parentId);
	}
	
	public Forum getForumById(int forumId) throws Exception{
		String sql = "select * from him_forum_forum where forumId = ?";
		return getJavaBean(sql, Forum.class, forumId);
	}
	
	public PageModel getPagedInfos(PageModel model,String search,int sortId) throws Exception{
		StringBuilder querySQL = new StringBuilder("select * from (select a.*,b.sortName from him_forum_forum a,him_live_sort b where a.sortId = b.sortId) as c");
		StringBuilder countSQL = new StringBuilder("select count(*) from (select a.*,b.sortName from him_forum_forum a,him_live_sort b where a.sortId = b.sortId) as c ");
		StringBuilder whereSQL = new StringBuilder(" where name like ?  ");
		List<Object> paramList = new ArrayList<Object>();
		paramList.add("%"+search+"%");
		if(sortId > 0){
			whereSQL.append(" and sortId = ?");
			paramList.add(sortId);
		}
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, Forum.class);
	}
	
	public List<Forum> getGroupsBySortId(int sortId) throws Exception{
		String sql = "select * from him_forum_forum where status = 2 and sortId = ?";
		return getList(sql, Forum.class, sortId);
		
	}
}
